<template>
  <div>
    <div class="search">
      <Row>
        <Col>
          <Card>
            <Row>
              <incomeChart />
            </Row>
            <Row>
              <Form ref="searchForm" :model="searchForm" inline :label-width="100" class="search-form">
                <FormItem label="项目部" prop="my_organization_id" >
                  <Select v-model="searchForm.my_organization_id" clearable filterable  @on-change="getOrganizationId">
                    <Option v-for="item in organizationList" :value="item.id" :key="item.id">{{ item.organization_name }}</Option>
                  </Select>
                </FormItem>
                <Form-item style="margin-left:-100px;" class="br">
                  <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                  <Button @click="handleReset">重置</Button>
                  <Button @click="viewAttachment" type="error" icon="ios-bookmarks-outline" >查看附件</Button>
                </Form-item>
              </Form>
            </Row>
            <Row>
              <Alert show-icon>
                已选择 <span class="select-count">{{selectCount}}</span> 项
                <a class="select-clear" @click="clearSelectAll">清空</a>
              </Alert>
            </Row>
            <Row>
              <Table
                border
                :loading="loading"
                :columns="columns"
                :data="data"
                sortable="custom"
                @on-selection-change="showSelect"
                ref="table">
              </Table>
              <Table :columns="exportColumns" :data="exportData" ref="exportTable" style="display:none"></Table>
            </Row>
            <Row type="flex" justify="end" class="page">
              <Page
                :current="searchForm.pageNumber"
                :total="total"
                :page-size="searchForm.pageSize"
                @on-change="changePage"
                @on-page-size-change="changePageSize"
                :page-size-opts="[10,20,50]"
                size="small"
                show-total
                show-elevator
                show-sizer
              ></Page>
            </Row>
          </Card>
        </Col>
      </Row>
      <Modal v-model="incomeVisiable" title="工程合同回款信息"  :styles="{top: '150px'}">
        <table width="90%">
          <tr v-for="(item,index) in income" :key="index">
            <td>
              客户名称: {{ item.customer_name }} <br/>
              开户银行: {{ item.bank_name }} <br/>
              银行帐号: {{ item.bank_account }} <br/>
              回款金额: {{ item.income }} <br/>
              回款日期: {{ item.incode_date }} <br/>
              附言: {{ item.content }} <br/>
              添加人: {{ item.real_name }} <br/><br/>
            </td>
            <hr/>
          </tr>
        </table>
      </Modal>
      <attachment v-model="attachmentVisiable" :data="attachmentValue"/>
    </div>
  </div>
</template>
<script>
import util from '@/libs/util';
import { getHeadquartersContract, getAllOrganization, getIncome } from '@/api/contract';
import incomeChart from './incomeChart';
import attachment from '../../attachment/attachmentManager';
export default {
  name: 'contract-approval',
  components: {
    incomeChart, attachment
  },
  data() {
    return {
      loading: false,
      searchForm: {
        pageNumber: 1,
        pageSize: 10,
        my_organization_id: 0
      },
      organizationList: [],
      income: [],
      incomeVisiable: false,
      inputVisiable: false,
      viewVisiable: false,
      attachmentVisiable: false,
      contract_id: 0,
      data: [],
      total: 0,
      selectCount: 0,
      exportData: [],
      selectList: [],
      exportColumns: [],
      columns:[
        {
          type: "selection",
          width: 50,
          align: "center",
          fixed: "left"
        },
        {
          type: "index",
          width: 50,
          align: "center",
          fixed: "left"
        },
        {
          title: '项目部',
          key: 'organization_name',
          width: 100
        },
        {
          title: '合同编码',
          key: 'contract_code',
          width: 190
        },
        {
          title: '合同名称',
          key: 'contract_name',
          width: 200,
        },
        {
          title: '项目编号',
          key: 'project_code',
          width: 140
        },
        {
          title: '甲方',
          key: 'company',
          width: 160
        },
        {
          title: '甲方账号',
          key: 'bank_account',
          width: 150
        },
        {
          title: '签订日期',
          key: 'contract_date',
          align: 'center',
          width: 100
        },
        {
          title: '合同价款',
          key: 'contract_cost',
          align: "right",
          width: 90,
        },
        {
          title: '合同回款',
          key: 'income',
          width: 70,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'text',
                  size: 'small'
                },
                style: {
                  marginRight: '5px',
                  color: 'red'
                },
                on: {
                  click: () => {
                    this.viewIncome(params.row)
                  }
                }
              }, '回款'),
            ]);
          }
        },
        {
          title: '工期开始',
          key: 'start_date',
          align: 'center',
          width: 90
        },
        {
          title: '工期结束',
          key: 'end_date',
          align: 'center',
          width: 90
        },
      ],
      form: {}
    }
  },
  methods: {
    getOrganizations() {
      getAllOrganization().then(res => {
        if (res.code === 0) {
          this.organizationList = res.data.rows;
        }
      })
    },
    getContractList() {
      this.loading = true;
      getHeadquartersContract(this.searchForm).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.data = res.data.rows;
          this.total = res.data.count;
        }
      });
    },
    refresh() {
      this.getContractList();
    },
    init() {
      this.getOrganizations();
      this.getContractList();
    },
    handleSearch() {
      if (this.searchForm.my_organization_id == 0) {
        this.$Message.info('请选择要查询的项目部');
        return;
      } else {
        this.getContractList();
      }
    },
    handleReset() {
      this.searchForm.my_organization_id = 0;
      this.pageNumber = 1;
      this.pageSize = 10;
      this.getContractList();
    },
    getOrganizationId(v) {
      this.searchForm.my_organization_id = v == undefined ? 0 : v;
    },
    showSelect(e) {
      this.exportData = e;
      this.selectList = e;
      this.selectCount = e.length;
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getContractList();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getContractList();
    },
    viewIncome(v) {
      const project_contract_id = v.id;
      getIncome({project_contract_id}).then(res => {
        if (res.code === 0 && res.data.length > 0) {
          this.income = res.data;
          this.incomeVisiable = true;
        } else {
          this.income = [];
          this.$Message.error('此工程合同还未回款');
          return;
        }
      })
    },
    viewAttachment() {
      if (this.selectList.length !== 1) {
        this.$Message.warning('必须选择一条记录进行操作!');
        return;
      }
      this.attachmentValue = {
        project_id: this.selectList[0].project_id,
        type: 1,
        showAll: false,
        showUploads: false
      }
      this.attachmentVisiable = true;
    }
  },
  mounted() {
    this.init();
  }
}
</script>
<style lang="less" scoped>
@import "./contractApproval.less";
</style>